import { useUserStore } from '@/store/user'

// 权限指令 v-permission
export const permission = {
  mounted(el, binding) {
    const { value } = binding
    const userStore = useUserStore()
    
    // test_admin 用户不受任何权限限制
    if (userStore.name === 'test_admin') {
      return
    }
    
    if (value && typeof value === 'string') {
      const hasPermission = userStore.hasPermission(value)
      
      if (!hasPermission) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    } else if (value && Array.isArray(value)) {
      const hasPermission = value.some(permission => userStore.hasPermission(permission))
      
      if (!hasPermission) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    } else {
      throw new Error('Need permission! Like v-permission="\'add:user\'" or v-permission="[\'add:user\', \'edit:user\']"')
    }
  }
}

// 测试账号只能查看，不能操作指令 v-test-account
export const testAccount = {
  mounted(el, binding) {
    const userStore = useUserStore()
    
    // test_admin 用户不受任何限制
    if (userStore.name === 'test_admin') {
      return
    }
    
    if (userStore.isTest) {
      // 检查是否是查看按钮（通常含有view文本）
      const isViewButton = el.textContent && (
        el.textContent.includes('查看') || 
        el.textContent.includes('详情') || 
        el.textContent.includes('预览')
      )
      
      // 如果不是查看按钮，则禁用
      if (!isViewButton && binding.value !== false) {
        // 禁用元素
        el.disabled = true
        el.classList.add('is-disabled')
        
        // 阻止点击事件
        el.addEventListener('click', (e) => {
          e.stopPropagation()
          e.preventDefault()
        }, true)
      }
    }
  }
}

// 权限按钮 v-has-perm
export const hasPerm = {
  mounted(el, binding) {
    const { value } = binding
    const userStore = useUserStore()
    
    // test_admin 用户不受任何限制
    if (userStore.name === 'test_admin') {
      return
    }
    
    // 如果是测试账号
    if (userStore.isTest) {
      // 判断是否为只读操作，只读操作允许测试账号访问
      const isReadOperation = value && typeof value === 'string' && (
        value.startsWith('view:') || 
        value === 'dashboard' || 
        value.includes('list') || 
        value.includes('view')
      )
      
      // 检查元素是否为查看按钮
      const isViewButton = el.textContent && (
        el.textContent.includes('查看') || 
        el.textContent.includes('详情') || 
        el.textContent.includes('预览') ||
        el.textContent.includes('列表')
      )
      
      // 如果既不是只读操作也不是查看按钮，则移除元素
      if (!isReadOperation && !isViewButton) {
        el.parentNode && el.parentNode.removeChild(el)
        return
      }
    }
    
    if (value && typeof value === 'string') {
      const hasPermission = userStore.hasPermission(value)
      
      if (!hasPermission) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    } else if (value && Array.isArray(value)) {
      const hasPermission = value.some(permission => userStore.hasPermission(permission))
      
      if (!hasPermission) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    }
  }
}

export default {
  install(app) {
    app.directive('permission', permission)
    app.directive('test-account', testAccount)
    app.directive('has-perm', hasPerm)
  }
} 